package com.longma.server.controller;

import com.longma.server.constant.enums.PTZDirectionEnum;
import com.longma.server.constant.enums.PTZMirrorEnum;
import com.longma.server.constant.enums.PTZSpeedEnum;
import com.longma.server.constant.enums.ProtocolEnum;
import com.longma.server.pojo.UserResponseEntity;
import com.longma.server.pojo.vo.YingshiAddressVO;
import com.longma.server.pojo.vo.YingshiTokenVO;
import com.longma.server.service.IYingshiService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

@RestController
@Validated
@RequestMapping("/api/ys")
public class YingshiController {
    @Autowired
    private IYingshiService yingshiService;

    @ApiOperation(value = "获取accessToken")
    @GetMapping("/token/get")
    public UserResponseEntity<YingshiTokenVO> getAccessToken(){
        return yingshiService.getAccessToken();
    }

    @ApiOperation(value = "获取播放地址")
    @GetMapping("/v2/live/address/get")
    public UserResponseEntity<YingshiAddressVO> getAddress(@ApiParam(value = "授权accessToken") @RequestParam("accessToken") @NotBlank(message = "授权accessToken不能为空") String accessToken,
                                                           @ApiParam(value = "设备序列号")  @RequestParam("deviceSerial") @NotBlank(message = "设备序列号不能为空")  String deviceSerial,
                                                           @ApiParam(value = "如果视频进行了加密 ezopen协议地址的设备的视频加密密码")  @RequestParam(value = "validateCode",required = false)   String validateCode,
                                                           @ApiParam(value = "流播放协议")  @RequestParam(value = "protocol",required = false)   ProtocolEnum protocol,
                                                           @ApiParam(value = "是否要求视频为H265编码格式")  @RequestParam(value = "supportH265",required = false)   Boolean supportH265){
        return yingshiService.getAddress(accessToken,deviceSerial,validateCode,protocol,supportH265);
    }

    @ApiOperation(value = "添加设备到账号下")
    @GetMapping("/device/add")
    public UserResponseEntity<String> addDevice(@ApiParam(value = "授权accessToken") @RequestParam("accessToken") @NotBlank(message = "授权accessToken不能为空")  String accessToken,
                                                        @ApiParam(value = "设备序列号") @RequestParam("deviceSerial") @NotBlank(message = "设备序列号不能为空") String deviceSerial,
                                                        @ApiParam(value = "设备验证码，设备机身上的六位大写字母") @RequestParam("validateCode") @NotBlank(message = "设备验证码不能为空") String validateCode) {
        return yingshiService.addDevice(accessToken,deviceSerial,validateCode);
    }

    @ApiOperation(value = "开始云台控制")
    @GetMapping("/device/ptz/start")
    public UserResponseEntity<String> startPTZControl(@ApiParam(value = "授权accessToken") @RequestParam("accessToken") @NotBlank(message = "授权accessToken不能为空") String accessToken,
                                                              @ApiParam(value = "设备序列号") @RequestParam("deviceSerial") @NotBlank(message = "设备序列号不能为空") String deviceSerial,
                                                              @ApiParam(value = "云台方向") @RequestParam("direction") @NotBlank(message = "云台方向不能为空") PTZDirectionEnum direction,
                                                              @ApiParam(value = "云台速度") @RequestParam("speed") @NotBlank(message = "云台速度不能为空") PTZSpeedEnum speed) {
        return yingshiService.startPTZControl(accessToken,deviceSerial,direction,speed);
    }

    @ApiOperation(value = "停止云台控制")
    @GetMapping("/device/ptz/stop")
    public UserResponseEntity<String> stopPTZControl(@ApiParam(value = "授权accessToken") @RequestParam("accessToken") @NotBlank(message = "授权accessToken不能为空") String accessToken,
                                                             @ApiParam(value = "设备序列号") @RequestParam("deviceSerial") @NotBlank(message = "设备序列号不能为空") String deviceSerial,
                                                             @ApiParam(value = "云台方向") @RequestParam("direction") @NotBlank(message = "云台方向不能为空") PTZDirectionEnum direction){
        return yingshiService.stopPTZControl(accessToken,deviceSerial,direction);
    }


    @ApiOperation(value = "对设备进行镜像翻转操作(需要设备支持")
    @GetMapping("/device/ptz/mirror")
    public UserResponseEntity<String> mirrorPTZ(@ApiParam(value = "授权accessToken") @RequestParam("accessToken") @NotBlank(message = "授权accessToken不能为空") String accessToken,
                                                @ApiParam(value = "设备序列号") @RequestParam("deviceSerial") @NotBlank(message = "设备序列号不能为空") String deviceSerial,
                                                @ApiParam(value = "镜像方向") @RequestParam("command") @NotBlank(message = "镜像方向不能为空") PTZMirrorEnum command){
        return yingshiService.mirrorPTZ(accessToken,deviceSerial,command);
    }

    @ApiOperation(value = "根据设备验证码关闭设备视频加密开关")
    @GetMapping("/device/encrypt/off")
    public UserResponseEntity<String> closeEncrypt(@ApiParam(value = "授权accessToken") @RequestParam("accessToken") @NotBlank(message = "授权accessToken不能为空")  String accessToken,
                                                   @ApiParam(value = "设备序列号") @RequestParam("deviceSerial") @NotBlank(message = "设备序列号不能为空") String deviceSerial,
                                                   @ApiParam(value = "设备验证码，设备机身上的六位大写字母") @RequestParam("validateCode") @NotBlank(message = "设备验证码不能为空") String validateCode) {
        return yingshiService.closeEncrypt(accessToken,deviceSerial,validateCode);
    }

    @ApiOperation(value = "开启设备视频加密")
    @GetMapping("/device/encrypt/on")
    public UserResponseEntity<String> openEncrypt(@ApiParam(value = "授权accessToken") @RequestParam("accessToken") @NotBlank(message = "授权accessToken不能为空")  String accessToken,
                                                  @ApiParam(value = "设备序列号") @RequestParam("deviceSerial") @NotBlank(message = "设备序列号不能为空") String deviceSerial) {
        return yingshiService.openEncrypt(accessToken,deviceSerial);
    }
}
